import util from '../../util/index.js'
let curPage;
let isEnd = false;
/**
 * 我的订单
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNoData: false,
    data: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  onReachBottom() {
    this.getData(++curPage);
  },
  onShow() {
    this.getData(1);
  },
  getData(page) {
    if (page == 1) {
      isEnd = false
    }
    if (isEnd) {
      return;
    }
    curPage = page
    wx.showNavigationBarLoading();
    util.ajax("order", {
      data: {
        action: "user",
        page,
      }
    }).then(({
      code,
      data
    }) => {
      wx.hideNavigationBarLoading();
      if (code == 1) {
        if (data.length == 0) {
          isEnd = true;
          if (page == 1) {
            this.setData({
              isNoData: true,
              data:[],
            })
          }
        } else {
          if (page == 1) {
            this.setData({
              data,
              isNoData: false,
            })
          } else {
            this.data.data.push(...data)
            this.setData({
              data: this.data.data,
              isNoData: false,
            })
          }
        }
      }
    }, wx.hideNavigationBarLoading)
  },
  cancel({
    currentTarget: {
      dataset: {
        index
      }
    }
  }) {
    wx.showModal({
      title: '提示',
      content: '是否取消预约？',
      success: ({
        confirm
      }) => {
        console.log(confirm)
        if (confirm) {
          util.ajax("order", {
            data: {
              action: "cancel",
              id: this.data.data[index]._id
            }
          }).then(({
            code,
            msg
          }) => {
            if (code == 1) {
              util.tip("取消预约成功", "success");
              this.setData({
                [`data[${index}].status`]: 2
              })
            } else {
              util.tip(msg);
            }
          })
        }
      }
    })

  }
})